package com.takeaway.service.serviceimpl;

import com.takeaway.const_.Const;
import com.takeaway.dao.daointerface.MsgBoardDao;
import com.takeaway.dao.daointerface.UserDao;
import com.takeaway.dao.daolmpl.MsgBoardImpl;
import com.takeaway.dao.daolmpl.UserDaoImpl;
import com.takeaway.pojo.MsgBoard;
import com.takeaway.pojo.User;
import com.takeaway.service.serviceinterface.MsgBoardService;

import java.util.ArrayList;
import java.util.List;


/**
 * @author 后台赖骏
 */
public class MsgBoardServiceImpl implements MsgBoardService {
    private static final MsgBoardDao MSG_DAO = new MsgBoardImpl();
    private static final UserDao USER_DAO = new UserDaoImpl();

    @Override
    public Object[] showMsg(int shopId) throws Exception {
        String[] checkField1 = {Const.FIELD_SHOP_ID};
       List<MsgBoard> msgBoards = MSG_DAO.selectMore(MsgBoard.class, checkField1, shopId);
       MsgBoard[] replyMsgBoards = new MsgBoard[msgBoards.size()];
       List<User> users = new ArrayList<>();
        String[] checkField2 = {Const.FIELD_ID};
        for (int i = 0; i < msgBoards.size(); i++) {
            MsgBoard m = msgBoards.get(i);
            if (m.getReplyId() != 0){
                MsgBoard msgBoard = MSG_DAO.selectSingle(new MsgBoard(), checkField2, m.getReplyId());
                replyMsgBoards[i] = msgBoard;
            }
            users.add(USER_DAO.selectSingle(new User(),checkField2,m.getUserId()));
        }
        Object[] objects = new Object[3];
        objects[0] = msgBoards;
        objects[1] = replyMsgBoards;
        objects[2] = users;
        return objects;
    }

    @Override
    public int deleteMsg(int msgBoardId) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        return MSG_DAO.delete(MsgBoard.class,checkField,msgBoardId);
    }

    @Override
    public int addMsg(int userId, int shopId, int replyId, String text) throws Exception {
        return MSG_DAO.insert(new MsgBoard(text,userId,shopId,replyId));
    }
}
